#include <QCoreApplication>
#include "Link.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //初始化SDK
    Link::init();

    //创建并打开第一个视频通道
    LinkObject *vi_1=Link::create("InputVi");
    QVariantMap dataVi_1;
    dataVi_1["interface"]=Link::getConfig()["interfaceV"].toMap().firstKey();
    vi_1->start(dataVi_1);

    //创建并打开第一个音频通道
    LinkObject *ai_1=Link::create("InputAi");
    QVariantMap dataAi_1;
    dataAi_1["interface"]=dataVi_1["interface"];//使用视频通道1对应的音频
    ai_1->start(dataAi_1);

    LinkObject *vi_2;
    LinkObject *ai_2=Link::create("InputAi");
    QVariantMap dataAi_2;

    //如果有第二个视频输入口就用
    if(Link::getConfig()["interfaceV"].toMap().keys().count()>1)
    {
        vi_2=Link::create("InputVi");
        QVariantMap dataVi_2;
        dataVi_2["interface"]=Link::getConfig()["interfaceV"].toMap().keys()[1];
        vi_2->start(dataVi_2);

        dataAi_2["interface"]=dataVi_2["interface"];//使用视频通道2对应的音频
    }
    else
    {
        //否则，将第一路视频裁剪放大，充当第二路视频
        vi_2=Link::create("Crop");
        QVariantMap crop;
        crop["x"]=0.25;
        crop["y"]=0.25;
        crop["width"]=0.5;
        crop["height"]=0.5;
        crop["pass"]=false;
        vi_2->start(crop);
        vi_1->linkV(vi_2);

        dataAi_2["interface"]="Line-In";//使用Line-In的音频
    }

    ai_2->start(dataAi_2);

    //创建混音模块
    LinkObject *mixA=Link::create("MixA");
    QVariantMap dataMixA;
    dataMixA["main"]=ai_1->name();
    mixA->start(dataMixA);

    //创建视频混合模块
    LinkObject *mixV=Link::create("MixV");
    //控制视频混合布局的参数，具体参考模块文档
    QVariantMap dataMixV;
    QVariantList srcs;
    QVariantList layouts;
    QVariantMap lay;
    lay["x"]=0;
    lay["y"]=0;
    lay["w"]=0.6;
    lay["h"]=0.6;
    lay["a"]=1;
    lay["index"]=0;
    layouts<<lay;
    lay["x"]=0.4;
    lay["y"]=0.4;
    lay["index"]=1;
    layouts<<lay;
    dataMixV["layout"]=layouts;
    srcs<<vi_1->name()<<vi_2->name();
    dataMixV["src"]=srcs;
    mixV->start(dataMixV);

    //创建一个视频输出模块
    LinkObject *vo=Link::create("OutputVo");
    QVariantMap dataVo;
    dataVo["type"]="hdmi";
    vo->start(dataVo);

    //创建一个音频输出模块
    LinkObject *ao=Link::create("OutputAo");
    QVariantMap dataAo;
    dataAo["interface"]="HDMI-OUT";
    ao->start(dataAo);

    //连接音频数据流(混音并输出)
    ai_1->linkA(mixA);
    ai_2->linkA(mixA)->linkA(ao);

    //连接视频数据流(视频混合并输出)
    vi_1->linkV(mixV);
    vi_2->linkV(mixV)->linkV(vo);

    return a.exec();
}

